<template>
  <div class="navbar">
    <div class="navbar-left">
      <!-- 侧边栏折叠按钮 -->
      <el-button 
        type="text" 
        @click="toggleSidebar"
        class="sidebar-toggle"
      >
        <el-icon>
          <Fold v-if="!sidebarCollapsed" />
          <Expand v-else />
        </el-icon>
      </el-button>

      <!-- 面包屑导航 -->
      <el-breadcrumb separator="/" class="breadcrumb">
        <el-breadcrumb-item 
          v-for="item in breadcrumbs" 
          :key="item.path"
          :to="item.path"
        >
          {{ item.meta?.title }}
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="navbar-right">
      <!-- 主题切换 -->
      <el-tooltip :content="isDark ? '切换为浅色' : '切换为深色'" placement="bottom">
        <el-switch
          v-model="isDark"
          @change="toggleTheme"
          class="theme-switch"
          inline-prompt
          :active-icon="Moon"
          :inactive-icon="Sunny"
        />
      </el-tooltip>

      <!-- 用户信息 -->
      <el-dropdown @command="handleCommand">
        <span class="user-info">
          <el-avatar :size="32" class="user-avatar">
            <el-icon><User /></el-icon>
          </el-avatar>
          <span class="user-name">{{ userInfo.nickname || '管理员' }}</span>
          <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
        </span>

        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="profile">个人中心</el-dropdown-item>
            <el-dropdown-item command="settings">系统设置</el-dropdown-item>
            <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAppStore } from '@/stores/app'
import { useAuthStore } from '@/stores/auth'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Fold, Expand, User, ArrowDown, Moon, Sunny } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const appStore = useAppStore()
const authStore = useAuthStore()

const sidebarCollapsed = computed(() => appStore.sidebarCollapsed)
const userInfo = computed(() => authStore.userInfo)
const isDark = computed({
  get: () => appStore.isDark,
  set: (v) => (appStore.isDark = v)
})

// 面包屑导航
const breadcrumbs = computed(() => {
  let matched = route.matched.filter(item => item.meta && item.meta.title)
  if (matched[0]?.path !== '/dashboard') {
    matched = [{ path: '/dashboard', meta: { title: '首页' } }, ...matched]
  }
  return matched
})

// 切换侧边栏
const toggleSidebar = () => {
  appStore.toggleSidebar()
}

// 切换主题
const toggleTheme = () => {
  appStore.toggleTheme()
}

// 处理用户下拉菜单
const handleCommand = async (command) => {
  switch (command) {
    case 'profile':
      ElMessage.info('个人中心功能开发中')
      break
    case 'settings':
      router.push('/settings')
      break
    case 'logout':
      try {
        await ElMessageBox.confirm('确认退出登录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        authStore.logout && authStore.logout()
        router.push('/auth/login')
        ElMessage.success('已退出登录')
      } catch {
        // 用户取消
      }
      break
  }
}
</script>

<style scoped>
.navbar {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: var(--header-bg);
  border-bottom: 1px solid var(--header-border);
}

.navbar-left {
  display: flex;
  align-items: center;
  min-width: 0;
}

.sidebar-toggle {
  margin-right: 12px;
  font-size: 18px;
}

/* 面包屑 */
.breadcrumb :deep(.el-breadcrumb__inner) {
  color: var(--muted-text) !important;
}
.breadcrumb :deep(.el-breadcrumb__inner.is-link) {
  color: var(--app-text) !important;
}

/* 右侧 */
.navbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

/* 主题开关 */
.theme-switch :deep(.el-switch__core) {
  border-color: var(--card-border);
  background: var(--card-bg);
}

/* 用户信息 */
.user-info {
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  user-select: none;
}
.user-avatar {
  margin-right: 8px;
  background: var(--primary-subtle);
  color: var(--primary);
}
.user-name {
  margin-right: 4px;
  color: var(--app-text);
}
.dropdown-icon {
  color: var(--muted-text);
}
</style>